/**
 * Application Application.java
 */

package CCT220_Aula19_Exercicio02_13106;


import java.awt.GridLayout;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


/**
 * </br>******************************
 * </br>Project: CCT220
 * </br>Package: CCT220_Aula19_Exercicio02_13106
 * </br>Class: Application
 * </br>Last Change: 28/10/2011 19:13:58 by marcel
 * </br>Description:
 * </br>******************************</br>
 * 
 * @author marcel
 * @author 13106
 * 
 */
public class Application implements ListSelectionListener {

	private JList            jlist1;
	private JList            jlist2;

	private DefaultListModel dlm1;
	private DefaultListModel dlm2;


	public Application() {

		this.startComponents();
		JFrame jf = new JFrame("Marcel M. Grilo - 13106");
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setLayout(new GridLayout(1, 2, 2, 2));
		jf.setSize(300, 200);
		jf.add(new JScrollPane(jlist1));
		jf.add(new JScrollPane(jlist2));
		jf.setVisible(true);
	}


	private void startComponents() {

		dlm1 = new DefaultListModel();
		dlm2 = new DefaultListModel();

		for (int i = 0; i < 5; i++) {
			dlm1.addElement("list01-item0" + (i + 1));
			dlm2.addElement("list02-item0" + (i + 1));
		}

		jlist1 = new JList(dlm1);
		jlist2 = new JList(dlm2);

		jlist1.addListSelectionListener(this);
		jlist2.addListSelectionListener(this);
	}


	public static void main(String[] args) {

		new Application();
	}


/*
 * (non-Javadoc)
 * 
 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.
 * ListSelectionEvent)
 */
	@Override
	public void valueChanged(ListSelectionEvent e) {

		if (e.getSource() == jlist1) {

			if (e.getValueIsAdjusting() == false) {
				int i = jlist1.getSelectedIndex();
				if (i == -1) { return; }
				dlm2.addElement(jlist1.getSelectedValue());
				dlm1.remove(i);
			}

		} else if (e.getSource() == jlist2) {
			if (e.getValueIsAdjusting() == false) {
				int i = jlist2.getSelectedIndex();
				if (i == -1) { return; }
				dlm1.addElement(jlist2.getSelectedValue());
				dlm2.remove(i);
			}
		}
	}
}
